#include "graphicsview.h"
#include <QDebug>
#include "times.h"
#include <QThread>
#include <QMouseEvent>
#include "itemmimedata.h"
#include "graphicstextitem.h"
#include "graphicsscene.h"
#include "keystatus.h"
#include "dragdrop.h"
#include "draws.h"

GraphicsView *gView = NULL;
GraphicsView::GraphicsView(QWidget *parent)
	: QGraphicsView(parent)
{
	gView = this;

	setAcceptDrops(true);
}

GraphicsView::~GraphicsView()
{
	//this->mousePressEvent()
	setMouseTracking(true);
}

void GraphicsView::mousePressEvent(QMouseEvent * event)
{
	Times::outCurrentTime("view");
	qDebug() << "[view].mousePressEvent() enter."
		<<"("<<event->pos().x()<<","<<event->pos().y()<<")";
	QThread::usleep(1);

	if (gDragDrop->viewMousePressEvent(event))
	{
		return;
	}

	QGraphicsView::mousePressEvent(event);
}
void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
	if (gDragDrop->viewMouseReleaseEvent(event))
	{
		return;
	}
	QGraphicsView::mouseReleaseEvent(event);
}
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
	if (gDragDrop->viewMouseMoveEvent(event))
	{
		return;
	}
	QGraphicsView::mouseMoveEvent(event);
}
void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
	QGraphicsView::dragEnterEvent(event);
	return;

	qDebug("->GraphicsView.dragEnterEvent().");
}
void GraphicsView::dragMoveEvent(QDragMoveEvent *event)
{
	QGraphicsView::dragMoveEvent(event);
	return;

	qDebug("->GraphicsView.dragMoveEvent().");
}
void GraphicsView::dropEvent(QDropEvent *event)
{
	QGraphicsView::dropEvent(event);
	return;

	qDebug("->GraphicsView.dropEvent().");
	gDragDrop->viewDropEvent(event);
}

void GraphicsView::keyPressEvent(QKeyEvent *event)
{
	if(event->key() == Qt::Key_Control)
		gKeyStatus->setControl(true);
	QGraphicsView::keyPressEvent(event);
}
void GraphicsView::keyReleaseEvent(QKeyEvent *event)
{
	if(event->key() == Qt::Key_Control)
		gKeyStatus->setControl(false);
	QGraphicsView::keyReleaseEvent(event);
}

// --- draw;
void GraphicsView::paintEvent(QPaintEvent *event)
{
	QGraphicsView::paintEvent(event);

	gDraws->viewPaintEvent(event);

	//qDebug()<<"[view].paint"<<gDragDrop->getDrawRect();

	//if (gDragDrop->getDrawRect())
	//{
	//	QPainter painter(this->viewport()); 
	//	QPointF p = gDragDrop->getPressPos();
	//	QPointF px = gDragDrop->getMovePos();
	//	painter.drawRect(p.x(),p.y(), px.x()-p.x(),px.y()-p.y());
	//	//qDebug()<<"-[view].paintEvent"
	//	//	<<p.x()<<p.y()<<px.x()<<px.y();

	//	//painter.drawLine(80, 100, 650, 500); 
	//	//painter.setPen(Qt::red); 
	//	//painter.drawRect(10, 10, 100, 400); 
	//	//painter.setPen(QPen(Qt::green, 5)); 
	//	//painter.setBrush(Qt::blue); 
	//	//painter.drawEllipse(50, 150, 400, 200); 

	//}
	
}